<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use common\components\helpers\Fun;
use doumiaoduo\layer\LayerIframeWidget;
use doumiaoduo\layer\LayerAsset;
LayerAsset::register($this);

LayerIframeWidget::widget(['button'=>"['添加','取消']"]);


?>
<div class="yx-praise-list-box yx-praise-box yx-radius2 zx-min-height" id="toolbar">
   <div>
       <div class="docs-main zx-mg">
           <table id="tablesaw" class="tablesaw table yx-table1 yx-table5 table-striped with-check"  data-tablesaw-mode="swipe" data-tablesaw-minimap >
               <thead>
               <tr class="yx-bgd2f6ec">
                   <th></th>
                    <th><?=\Yii::t('zhtfrontend','商品名称');?></th>
                    <th><?=\Yii::t('zhtfrontend','商品编号');?></th>
                    <th><?=\Yii::t('zhtfrontend','分类名称');?></th>
                    <th><?=\Yii::t('zhtfrontend','品牌名称');?></th>
                    <th><?=\Yii::t('zhtfrontend','机构');?></th>
                    <th><?=\Yii::t('zhtfrontend','库存');?></th>
                    <th><?=\Yii::t('zhtfrontend','价格');?></th>
                    <th><?=\Yii::t('zhtfrontend','成本价');?></th>
                    <th><?=\Yii::t('zhtfrontend','市场价');?></th>
                    <th><?=\Yii::t('zhtfrontend','是否可购买');?></th>
                    <th><?=\Yii::t('zhtfrontend','商品类型');?></th>
                   <th><?=\Yii::t('zhtfrontend','优惠类型');?></th>
                   <th><?=\Yii::t('zhtfrontend','销售价减少');?></th>
                   <th><?=\Yii::t('zhtfrontend','成本价减少');?></th>
                   <th><?=\Yii::t('zhtfrontend','市场价减少');?></th>
                </thead>
               <?php if(!empty($goodsgroup)):?>
                   <?php foreach($goodsgroup as $item):?>
                   <tr value="<?=$item['id']?>">
                       <td><input type="checkbox" name="goodsId" value="<?=$item['id']?>"></td>
                       <td><?=$item['name']?></td>
                       <td><?=$item['sn']?></td>
                       <td><?=$item['cate_name']?></td>
                       <td><?=$item['brand_name']?></td>
                       <td><?=$item['mechanic_name']?></td>
                       <td><?=$item['store']?></td>
                       <td><?=$item['price']?></td>
                       <td><?=$item['cost']?></td>
                       <td><?=$item['market_price']?></td>
                       <td><?=$item['is_buy']?></td>
                       <td><?=$item['goods_type']?></td>
                       <td><?=$item['discount_type']?></td>
                       <td><?=$item['price_reduce']?></td>
                       <td><?=$item['cost_reduce']?></td>
                       <td><?=$item['market_price_reduce']?></td>
                   </tr>
                   <?php endforeach;?>
               <?php endif;?>
            </table>

        </div>
        <div style="margin: 10px">
            <span>
                <a class='yx-radius2 yx-batch-settlement' id='addgroup' data-size='1300*500' rel="dialog-show-iframe" data-name="选择商品" data-url='<?=Fun::url('goodsgroup/addgroup');?>'  >添加商品</a>
                <a class='yx-batch-settlement yx-bg7ecef4 yx-ml10 yx-radius2' id="remove" >移除商品</a>
                <a class='yx-batch-settlement  yx-ml10 yx-radius2' id="confirm" >确认价格</a>
            </span>
        </div>
   </div>
    <?php $form = ActiveForm::begin(['enableAjaxValidation' => true, 'enableClientValidation' => false, 'options' => ['class' => 'form-validate']]); ?>
    <table width="100%" class="table-form">
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['name']; ?></th>
            <td><?= $form->field($model, 'name', ['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:300px','class' => 'input-text']) ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['cat_id']; ?></th>
            <td style="display: inline-block;width: 206px"><?= $form->field($model, 'cat_id', ['template' => '{input}{error}{hint}'])->widget(\kartik\select2\Select2::className(),['data'=>$cat_tree,'options' => ['placeholder' => '请选择']]) ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['brand_id']; ?></th>
            <td style="display: inline-block;width: 206px"><?= $form->field($model, 'brand_id', ['template' => '{input}{error}{hint}'])->widget(\kartik\select2\Select2::className(),['data'=>$brand_tree,'options' => ['placeholder' => '请选择']]) ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['price']; ?></th>
            <td><?= $form->field($model, 'price', ['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:180px', 'value' => 0, 'class' => 'input-text','readonly'=>true]) ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['cost']; ?></th>
            <td><?= $form->field($model, 'cost', ['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:180px', 'value' => 0, 'class' => 'input-text','readonly'=>true]); ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['market_price']; ?></th>
            <td><?= $form->field($model, 'market_price', ['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:180px', 'value' => 0, 'class' => 'input-text','readonly'=>true]); ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['store']; ?></th>
            <td><?= $form->field($model, 'store', ['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:180px', 'class' => 'input-text','readonly'=>true]); ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['picture']; ?></th>
            <td>
                    <?= $form->field($model, 'imageList', ['template' => '{input}{error}{hint}'])->widget(\doumiaoduo\ueditor\ImageUploadMultiple::className(),['options'=>[
                        'class'=>'input-text',
                    ],
                        'clientOptions'=>[
                            'serverUrl'=>\common\components\helpers\Fun::url(['/uploader/action'])
                        ],]); ?>
             </td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['description']; ?></th>
            <td><?= $form->field($model, 'description', ['template' => '{input}{error}{hint}'])->widget(\doumiaoduo\ueditor\Ueditor::className(), ['clientOptions'=>[
                    'serverUrl'=>\common\components\helpers\Fun::url(['/uploader/action']),
                    'toolbars'=>[['fullscreen', 'source', '|', 'undo', 'redo', '|','bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript','rowspacingtop', 'rowspacingbottom', 'lineheight', '|','justifyleft', 'justifycenter', 'justifyright', 'justifyjustify','simpleupload']],
                    'initialFrameWidth'=>'100%'
                ]]); ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['content']; ?></th>
            <td><?= $form->field($model, 'content', ['template' => '{input}{error}{hint}'])->widget(\doumiaoduo\ueditor\Ueditor::className(), ['clientOptions'=>[
                    'serverUrl'=>\common\components\helpers\Fun::url(['/uploader/action']),
                    'toolbars'=>[['fullscreen', 'source', '|', 'undo', 'redo', '|','bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript','rowspacingtop', 'rowspacingbottom', 'lineheight', '|','justifyleft', 'justifycenter', 'justifyright', 'justifyjustify','simpleupload']],
                    'initialFrameWidth'=>'100%'
                ]]); ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['mechanic_id']; ?></th>
            <td style="display: inline-block;width: 206px"><?= $form->field($model, 'mechanic_id', ['template' => '{input}{error}{hint}'])->widget(\kartik\select2\Select2::className(),['data'=>$mechanic_tree,'options' => ['placeholder' => '请选择']]) ?></td>
        <tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['max_buy']; ?></th>
            <td><?= $form->field($model, 'max_buy', ['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:180px', 'class' => 'input-text']); ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['score']; ?></th>
            <td><?= $form->field($model, 'score', ['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:180px', 'class' => 'input-text']); ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['store_prompt']; ?></th>
            <td><?= $form->field($model, 'store_prompt', ['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:180px', 'class' => 'input-text']); ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['nostore_sell']; ?></th>
            <td><?= $form->field($model, 'nostore_sell', ['template' => '{input}{error}{hint}'])->radioList(['N'=>'否','Y'=>'是'],['style' => 'width:180px', 'class' => 'radiolist']); ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['is_buy']; ?></th>
            <td><?= $form->field($model, 'is_buy', ['template' => '{input}{error}{hint}'])->radioList(['0'=>'否','1'=>'是'],['style' => 'width:180px', 'class' => 'radiolist']); ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['is_show']; ?></th>
            <td><?= $form->field($model, 'is_show', ['template' => '{input}{error}{hint}'])->radioList(['0'=>'否','1'=>'是'],['style' => 'width:180px', 'class' => 'radiolist']); ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['is_shelve']; ?></th>
            <td><?= $form->field($model, 'is_shelve', ['template' => '{input}{error}{hint}'])->radioList($shelve_array,['style' => 'width:180px', 'class' => 'radiolist']); ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['groupIds']; ?></th>
            <td><?= $form->field($model, 'groupIds', ['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:180px', 'class' => 'input-text','readonly'=>true]); ?></td>
        </tr>
        <?=$attribute?>
    </table>
    <div class="m_top_10 btn" style="width:100%">
        <?= Html::submitButton('保存', ['class' => "btn yx-goods-btn yx-radius5", 'id' => 'dosubmit']) ?>&nbsp;
        <a href=<?= Fun::url(['goodsgroup/index','type_id' => 5]) ?>><span class="button">返回</span></a>&nbsp;
    </div>
<?php ActiveForm::end(); ?>
</div>
<?php
$js = <<<JS

        $(document).on('click','.layui-layer-btn0',function(){
            var str = '',ids =[], hereids =[], nids =[];
               if($('.tablesaw input[name="goodsId"]').length > 0){
                 $('input[name="goodsId"]').each(function(){
                      hereids.push($(this).val());
                   });
          }
               $("iframe[id^=layui-layer-iframe]").contents().find('input[name="goodsId"]:checked').each(function(){
                     var parent = $(this).parents('tr');
                     var strs = parent.html();
                     var _this = $(this);
                    if($.inArray(_this.val(),hereids) == -1){
                       str += '<tr value='+_this.val()+'>';
                       str += strs;
                       str += '</tr>';
                       nids.push(_this.val());
                    }
                   ids.push(_this.val());
             });
                  if(ids.length == 0){
                     layer.msg("请先选择商品！",{id:'layer-msg',skin: 'layui-msg-customskin',time: 1000});
                     return false;
                   }else{
                    var newarr =  $.merge(nids,hereids);
                    $('#goods-groupids').val(newarr);
                    $('.tablesaw').append(str);
                    $(".layui-layer-btn1").click();
                 }
                 return false;
        });
    $(document).on('click','#remove',function(){
        var hereids = [],ids = [];
             if($('.tablesaw input[name="goodsId"]').length > 0){
             $('input[name="goodsId"]').each(function(){
                var parents = $(this).parents('tr');
               if($(this).is(':checked')){
                    hereids.push($(this).val());
                    parents.remove();
                 }else{
                     ids.push($(this).val());
                 }
               });
               if(hereids.length == 0){
                  layer.msg("请先选择要移除的商品！",{id:'layer-msg',skin: 'layui-msg-customskin',time: 1000});
                  return false;
               }
               $('#goods-groupids').val(ids);
          }

    });

    $(document).on('click','#confirm',function(){
        var price = 0,cost = 0,market_price = 0,store = 0,group_store = 0;

        $("#tablesaw tbody tr").each(function(){

            var goods_store = $(this).find('td').eq(6).html();
            var goods_price = $(this).find('td').eq(7).html();
            var goods_cost = $(this).find('td').eq(8).html();
            var goods_market_price = $(this).find('td').eq(9).html();

            var discount_type = $(this).find('select[name="discount_type"]').val();
            var price_reduce = $(this).find('input[name="price_reduce"]').val();
            var cost_reduce = $(this).find('input[name="cost_reduce"]').val();
            var market_price_reduce = $(this).find('input[name="market_price_reduce"]').val();

            if(discount_type == 1){
                var group_price = goods_price - (price_reduce * (price_reduce/100));
                var group_cost = goods_cost - (cost_reduce * (cost_reduce/100));
                var group_market_price = goods_market_price - (market_price_reduce * (market_price_reduce/100));
            }else if(discount_type == 2){
                var group_price = goods_price - price_reduce;
                var group_cost = goods_cost - cost_reduce;
                var group_market_price = goods_market_price - market_price_reduce;
            }

            price = price + parseFloat(group_price);
            cost = cost + parseFloat(group_cost);
            market_price = market_price + parseFloat(group_market_price);

            if(store == 0){
                store = goods_store;
            }else if(store != 0 && store > goods_store){
                store = goods_store;
                group_store = store;
            }else if(store != 0 && store < goods_store){
                group_store = store;
            }

            var data_id = $(this).attr('value');
            var data = discount_type + ',' + price_reduce + ',' + cost_reduce + ',' + market_price_reduce;
            $('.table-form tbody').append('<tr><th></th><td><input type="hidden" class="data" name="data[' +data_id +']" value="'+ data +'"></td></tr>');

        });

        $('.table-form input[id="goods-store"]').val(group_store);
        $('.table-form input[id="goods-price"]').val(price.toFixed(2));
        $('.table-form input[id="goods-cost"]').val(cost.toFixed(2));
        $('.table-form input[id="goods-market_price"]').val(market_price.toFixed(2));
    });
JS;
$this->registerJS($js);
?>

